<!DOCTYPE html>
<!--

                     /$$   /$$                         /$$
                    |__/  | $$                        | $$
  /$$$$$$   /$$$$$$  /$$ /$$$$$$   /$$   /$$ /$$$$$$$ | $$
 /$$__  $$ /$$__  $$| $$|_  $$_/  | $$  | $$| $$__  $$| $$
| $$  \ $$| $$  \__/| $$  | $$    | $$  | $$| $$  \ $$| $$
| $$  | $$| $$      | $$  | $$ /$$| $$  | $$| $$  | $$| $$
| $$$$$$$/| $$      | $$  |  $$$$/|  $$$$$$/| $$  | $$| $$
| $$____/ |__/      |__/   \___/   \______/ |__/  |__/|__/
| $$
| $$
|__/

-->
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Pritunl Database Setup</title>
    <meta name="viewport" content="width=600">
    <link rel="icon" type="image/x-icon" href="">
    <style type="text/css">
      @font-face {
        font-family: 'Fredoka One';
        font-style: normal;
        font-weight: 400;
        src: url('./setup/s/fredoka-one.eot') format('embedded-opentype'),
          url('./setup/s/fredoka-one.woff') format('woff');
      }

      @font-face {
        font-family: 'Ubuntu';
        font-style: normal;
        font-weight: 700;
        src: url('./setup/s/ubuntu-bold.eot') format('embedded-opentype'),
          url('./setup/s/ubuntu-bold.woff') format('woff');
      }

      *, *:before, *:after {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
      }

      body {
        background-color: #4d4d4d;
        font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
        font-size: 14px;
        line-height: 1.428571429;
        color: #333;
      }

      .form-control {
        display: block;
        width: 100%;
        padding: 14px 16px;
        font-size: 18px;
        color: #555;
        background-color: #fff;
        background-image: none;
        border: 1px solid #ccc;
        -webkit-border-radius: 2px;
        -moz-border-radius: 2px;
        -ms-border-radius: 2px;
        -o-border-radius: 2px;
        border-radius: 2px;
        -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
        box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
      }

      .form-control:focus {
        outline: 0;
      }

      .form-label {
        color: #2e4153;
        display: inline-block;
        font-weight: 700;
        font-family: 'Ubuntu', 'Verdana', 'Geneva', sans-serif;
        font-size: 18px;
        margin-bottom: 8px;
      }

      .modal-logo {
        width: 450px;
        margin: 40px 0;
      }

      .btn {
        display: inline-block;
        padding: 6px 12px;
        margin-bottom: 0;
        font-size: 16px;
        font-weight: normal;
        line-height: 1.42857143;
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        cursor: pointer;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        background-image: none;
        border: 1px solid transparent;
        border-radius: 4px;
        -webkit-border-radius: 2px;
        -moz-border-radius: 2px;
        -ms-border-radius: 2px;
        -o-border-radius: 2px;
        border-radius: 2px;
        color: #fff;
        background-color: #428bca;
        border-color: #357ebd;
      }

      .btn:focus,
      .btn:hover,
      .btn:active {
        outline: 0;
      }

      .btn:hover,
      .btn:active {
        outline: 0;
        color: #fff;
        background-color: #3071a9;
        border-color: #285e8e;
      }

      .btn:active {
        background-image: none;
        outline: 0;
        -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
        box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125);
      }

      .btn[disabled] {
        background-color: #428bca;
        border-color: #357ebd;
        pointer-events: none;
        cursor: not-allowed;
        filter: alpha(opacity=65);
        -webkit-box-shadow: none;
        box-shadow: none;
        opacity: .65;
      }

      .alert {
        display: none;
        padding: 10px;
        margin-top: 10px;
        margin-bottom: 20px;
        -webkit-border-radius: 2px;
        -moz-border-radius: 2px;
        border-radius: 2px;
        border: 1px solid transparent;
        border-radius: 4px;
      }

      .alert-success {
        color: #3c763d;
        background-color: #daeed1;
        border-color: #b2df8d;
      }

      .alert-warning {
        color: #8a6d3b;
        background-color: #fff8db;
        border-color: #f1d997;
      }

      .alert-danger {
        color: #a94442;
        background-color: #eed6d6;
        border-color: #e0a2ac;
      }

      .default-text {
        color: #616161 !important;
      }

      .error-text {
        color: #f00 !important;
      }

      .warning-text {
        color: #f90 !important;
      }

      .success-text {
        color: #3ba51a !important;
      }

      .modal-box {
        width: 500px;
        margin: 50px auto 50px auto;
        background-color: #f9f9f9;
        -webkit-border-radius: 2px;
        -moz-border-radius: 2px;
        -ms-border-radius: 2px;
        -o-border-radius: 2px;
        border-radius: 2px;
      }

      .modal-box .modal-header {
        margin: 0;
        padding: 10px 15px;
        border-top-left-radius: 2px;
        border-top-right-radius: 2px;
        background-color: #222;
        border-color: #080808;
        font-family: 'Fredoka One', 'Verdana', 'Geneva', cursive;
        font-size: 30px;
        color: #ececec;
        font-weight: 500;
        line-height: 1.1;
      }

      .modal-box .modal {
        padding: 20px;
        text-align: center;
      }

      .modal-box .modal button {
        margin-top: 15px;
        margin-bottom: 4px;
        min-width: 260px;
        padding: 10px 12px;
        font-size: 18px;
      }

      .modal-box .modal button {
        margin-top: 23px;
      }

      #setup-key {
        margin-bottom: 8px;
      }
    </style>
  </head>
  <body>
    <div class="modal-box">
      <h2 class="modal-header">pritunl database setup</h2>
      <div class="modal">
        <img class="modal-logo" src="">
        <div id="alert" class="alert"></div>
        <label class="form-label">Enter Setup Key (Run "sudo pritunl setup-key")</label>
        <input type="text" id="setup-key" class="form-control" placeholder="Enter setup key" data-toggle="tooltip" title="Run command 'sudo pritunl setup-key' to get setup key" value="" autofocus>
        <label class="form-label">Enter MongoDB URI</label>
        <input type="text" id="mongodb-uri" class="form-control" placeholder="mongodb://localhost:27017/pritunl" data-toggle="tooltip" title="MongoDB URI" value="mongodb://localhost:27017/pritunl">
        <button onclick="save()" id="save" class="modal-save btn">Save</button>
      </div>
    </div>
    <script type="text/javascript">
      var keyInput = document.getElementById('setup-key');
      var uriInput = document.getElementById('mongodb-uri');
      var saveBtn = document.getElementById('save');
      var alertDiv = document.getElementById('alert');

      var setAlert = function(alert, style) {
        if (alert) {
          alertDiv.className = 'alert alert-' + (style || 'success');
          alertDiv.innerHTML = alert;
          alertDiv.style.display = 'block';
        }
        else {
          alertDiv.style.display = 'none';
        }
      };

      var save = function() {
        saveBtn.disabled = true;
        keyInput.disabled = true;
        uriInput.disabled = true;

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
              setTimeout(function() {
                window.location = '/';
              }, 3500);
            }
            else {
              var errorMsg;
              try {
                errorMsg = JSON.parse(xmlhttp.responseText)['error_msg'];
              }
              catch(error) {
                errorMsg = 'Unknown server error occurred.';
              }
              setAlert(errorMsg, 'danger');
              saveBtn.disabled = false;
              keyInput.disabled = false;
              uriInput.disabled = false;
            }
          }
        };
        xmlhttp.open('PUT', '/setup/mongodb', true);
        xmlhttp.setRequestHeader('Content-Type', 'application/json');
        xmlhttp.send(JSON.stringify({
          'setup_key': keyInput.value,
          'mongodb_uri': uriInput.value
        }));
      };
    </script>
  </body>
</html>
